<template>
  <div class="container">
    <el-card>
      <el-row>
        <el-col :span="6">
          <div class="grid-content bg-purple">
            <span>目录名称</span>
            <el-input v-model="QueryData.directoryName" placeholder="请输入内容"></el-input>
          </div>
        </el-col>
        <el-col :span="15">
          <div class="grid-content bg-purple-light">
            <span>状态</span>
            <el-select v-model="QueryData.state" placeholder="请选择">
              <!-- <el-option v-for="(item, index) in arrs" :key="index" :value="item === 1 ? '启动' : '暂停'"></el-option> -->
              <el-option :value="1" label="启动">启动</el-option>
              <el-option :value="0" label="暂停">暂停</el-option>
            </el-select>
            <el-button size="small" @click="clearData">清除</el-button>
            <el-button type="primary" size="small" @click="search">搜索</el-button>
          </div>
        </el-col>
        <el-col :span="3">
          <el-button type="success" @click="addData">新增目录</el-button>
        </el-col>
      </el-row>
      <el-alert type="info" show-icon :closable="false">本次获取了{{ counts }}条数据</el-alert>
      <!-- 表格 -->
      <el-table :data="listdata" style="width: 100%">
        <el-table-column prop="id" label="序号" width="110" type="index"></el-table-column>
        <el-table-column prop="subjectName" label="所属学科" width="160"></el-table-column>
        <el-table-column prop="directoryName" label="目录名称" width="140"></el-table-column>
        <el-table-column prop="username" label="创建者" width="130"></el-table-column>
        <el-table-column prop="addDate" label="创建日期" width="180">
          <template slot-scope="scope">
            <span>{{ scope.row.addDate | parseTimeByString }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="totals" label="面试题数量" width="130"></el-table-column>
        <el-table-column prop="state" label="状态" width="180">
          <template slot-scope="scope">
            <span v-if="scope.row.state === 1">启动</span>
            <span v-if="scope.row.state === 0">暂停</span>
          </template>
        </el-table-column>
        <el-table-column prop="name" label="操作" width="180">
          <template slot-scope="scope">
            <el-button type="text" v-if="scope.row.state === 1" @click="goData(scope.row.id, scope.row.state)">禁用</el-button>&nbsp;
            <el-button type="text" v-else @click="goData(scope.row.id, scope.row.state)">启用</el-button>&nbsp;
            <el-button type="text" @click="deitData(scope.row.id)" :disabled="!!scope.row.state">修改</el-button>&nbsp;
            <el-button type="text" @click="deleteData(scope.row.id)" :disabled="!!scope.row.state">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="QueryData.page"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="100"
        layout="sizes, prev, pager, next, jumper"
        :total="counts"
      ></el-pagination>
      <!-- 修改弹框 -->
      <el-dialog title="修改目录" :visible.sync="dialogFormVisible" width="490px">
        <el-form :model="Dataform" :rules="rules">
          <el-form-item label="所属学科" :label-width="formLabelWidth">
            <el-select v-model="form.region" placeholder="请选择科目">
              <el-option v-for="(item, index) in listdata" :key="index" :label="item.directoryName" :value="item.subjectID"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="editok">确 定</el-button>
        </div>
      </el-dialog>
      <!-- /修改弹框 -->
      <!-- 新增弹框 -->
      <el-dialog title="新增目录" :visible.sync="addDatashow" width="490px">
        <el-form :model="Dataform" :rules="rules">
          <el-form-item label="所属学科" :label-width="formLabelWidth">
            <el-select v-model="form.region" placeholder="请选择科目">
              <el-option v-for="(item, index) in listdata" :key="index" :label="item.directoryName" :value="item.subjectID"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="目录名称" :label-width="formLabelWidth" prop="name">
            <el-input v-model="form.name" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="addDatashow = false">取 消</el-button>
          <el-button type="primary" @click="addok">确 定</el-button>
        </div>
      </el-dialog>
      <!-- /新增弹框 -->
    </el-card>
  </div>
</template>

<script>
import { createAPI } from '@/utils/request'

import { list, update, changeState, remove, detail, add } from '@/api/hmmm/directorys.js'
import { parseTimeByString } from '@/filters/index.js'
export default {
  filters: {
    parseTimeByString
  },
  data() {
    return {
      // 新增
      addDatashow: false,
      rules: {
        name: [
          { required: true, message: '请输入修改内容' },
          { min: 1, max: 6, message: '长度在 1 到 6 个字符' }
        ],
      },
      // 确定修改的数据
      resData: null,
      // 搜索内容
      inputData: '',
      // 选中项
      value: '',
      // 存放数据
      listdata: [],
      // 请求参数
      QueryData: {
        page: 1,
        pagesize: 10,
        directoryName: '',
        subjectID: null,
        state: null
      },
      counts: null,
      arrs: [],
      // 修改目录
      dialogFormVisible: false,
      Dataform: null,
      dialogTableVisible: false,

      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '120px'
    }
  },
  created() {
    this.getDataList()
  },
  methods: {
    handleSizeChange(val) {
      this.QueryData.pagesize = val
      this.getDataList()
    },
    handleCurrentChange(val) {
      this.QueryData.page = val
      this.getDataList()
    },
    async getDataList() {
      try {
        const Data = await list(this.QueryData)

        this.listdata = Data.data.items
        console.log(this.listdata)

        this.counts = Data.data.counts
        this.listdata = this.listdata.reverse()
        // 循环数组中的每一项，只需要其中一项
        const arr = []
        this.listdata.forEach((item) => {
          arr.push(item.state)
        })
        // console.log(arr)
        // this.arrs = arr
        // 去重
        const newArr = new Set(arr)
        this.arrs = newArr
      } catch (err) {
        console.log(err)
        this.$message('失败')
      }
    },
    clearData() {
      ;(this.inputData = ''), (this.value = '')
    },
    // 启动暂停项目
    async goData(id, state) {
      console.log(id, state)
      try {
        const data = await changeState({ id: id, state: state === 1 ? 0 : 1 })

        console.log(data)

        this.$message('修改成功')
        this.getDataList()
      } catch (err) {
        this.$message('修改失败，请稍后再试')
      }
    },
    // 删除
    async deleteData(id) {
      try {
        const data = await remove({ id: id })
        console.log(data)
        this.$message('删除成功')
        this.getDataList()
      } catch (err) {
        this.$message('删除失败，请稍后再试')
      }
    },
    // 修改
    // 获取题详情
    async deitData(id) {
      this.ids = id
      console.log(id)
      this.dialogFormVisible = true
      try {
        const { data: res } = await detail({ id: id })
        console.log(res)
        this.resData = res
        this.form.region = res.directoryName
        this.form.name = res.subjectName
        // console.log(this.Dataform)
      } catch (err) {
        console.log(err)
        this.$message('获取信息失败')
      }
    },
    async editok() {
      console.log(this.resData.subjectID, this.resData.id, this.form.name)
      this.dialogFormVisible = false
      if (!this.form.name) {
        this.$message('请输入目录名称')
        return false
      }
      try {
        const data = await update({ id: this.resData.id, subjectID: this.resData.subjectID, directoryName: this.form.name })

        console.log(data)
        if (data.status === 200) {
          this.$message('修改成功')
          this.form.name = ''

          this.getDataList()
        }
      } catch (err) {
        // console.log(err)
        this.$message('请求失败，请稍后再试')
      }
    },
    search() {
      this.getDataList()
    },
    addData() {
      this.addDatashow = true
    },
    async addok() {
      this.dialogFormVisible = false
      if (!this.form.name) {
        this.$message('请输入目录名称')
        return false
      }
      try {
        const data = await add({ subjectID: this.form.region, directoryName: this.form.name })

        console.log(data)
        if (data.status === 200) {
          this.$message('添加成功')
          this.form.name = ''
          this.addDatashow = false

          this.getDataList()
        }
      } catch (err) {
        console.log(err)
        this.$message('添加失败，请稍后再试')
      }
    }
  }
}
</script>

<style scoped>
.el-input {
  width: 200px;
  margin-left: 10px;
}
.el-select {
  margin-left: 10px;
}
.el-button {
  margin-left: 10px;
  height: 34px;
}
.el-alert {
  margin-top: 20px;
}
.el-pagination {
  margin-top: 20px;
  margin-bottom: 30px;
}
</style>
